package com.tupu.controller.dispatch;

import com.tupu.common.PageResult;
import com.tupu.common.Result;
import com.tupu.entity.dispatch.*;
import com.tupu.entity.dto.WarningEventFailurePlanDto;
import com.tupu.service.dispatch.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 告警事件
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/event")
public class WarningEventController {

    @Autowired(required = false)
    private WarningEventService warningEventService;

    @Autowired(required = false)
    private WarningEventOverhaulService warningEventOverhaulService;

    @Autowired(required = false)
    private PlanService planService;

    /**
     * 告警事件首页展示 条件搜索，分页
     *
     * @param page
     * @param size
     * @param eventType
     * @param station
     * @param deviceType
     * @param device
     * @return
     */
    @PostMapping("/eventList")
    public Result conditionSearchEventList(@RequestParam(required = false, defaultValue = "1") Long page,
                                           @RequestParam(required = false, defaultValue = "10") Long size,
                                           @RequestParam(required = false) String eventType,
                                           @RequestParam(required = false) String station,
                                           @RequestParam(required = false) String deviceType,
                                           @RequestParam(required = false) String startTime,
                                           @RequestParam(required = false) String endTime,
                                           @RequestParam(required = false) String device) {
        try {
            PageResult<WarningEvent> info = warningEventService.conditionSearch(page, size, eventType, station, deviceType, startTime, endTime, device);
            return Result.ok(info);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 展示所有事件类别
     *
     * @return
     */
    @PostMapping("/eventTypeList")
    public Result findAllEventType() {
        Map<String, Integer> map = new HashMap<>();
        map.put("事故告警", 0);
        map.put("异常告警", 1);
        return Result.ok(map);
    }

    /**
     * 展示所有厂站
     *
     * @return
     */
    @PostMapping("/stationList")
    public Result findAllStation() {
        Map<String, Integer> map = warningEventService.findAllStation();
        return Result.ok(map);
    }

    /**
     * 展示所有设备类型
     *
     * @return
     */
    @PostMapping("/deviceTypeList")
    public Result findAllDeviceType() {
        Map<String, String> map = warningEventService.findAllDeviceType();
        return Result.ok(map);
    }

    /**
     * 展示所有设备
     *
     * @return
     */
    @PostMapping("/deviceList")
    public Result findAllDevice() {
        Map<String, Integer> map = warningEventService.findAllDevice();
        return Result.ok(map);
    }

    /**
     * 关联的告警信号 详情
     *
     * @param
     * @return
     */
    @PostMapping("/contactSignalDataList")
    public Result contactSignalDataList(@RequestParam(required = false) String eventId) {
        List<WarningEventContactSignalData> list;
        try {
            list = warningEventService.findByEventId(eventId);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }

    /**
     * 信号缺失详情
     *
     * @param eventId
     * @return
     */
    @PostMapping("/missDataList")
    public Result findAllMissData(@RequestParam(required = false) String eventId) {
        try {
            List<WarningEventMissData> list = warningEventService.findMissData(eventId);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 故障预案识别导入
     *
     * @param file
     * @return
     */
    @PostMapping("/wordImport")
    public Result wordImport(@RequestParam(required = false) MultipartFile file,
                             @RequestParam(required = false) String eventId) {
        //文件后缀
        String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if (type.equals(".docx")) {
            try {
                warningEventService.uploadWordPlan(file, eventId);
                return Result.ok("上传成功");
            } catch (Exception e) {
                return Result.errorMessage(e.getMessage());
            }
        }
        return Result.errorMessage("请上传docx格式文档！");
    }

    @PostMapping("/planOneUpdate")
    public Result planOneUpdate(@RequestBody WarningEventFailurePlan plan) {
        try {
            //neo4jUtil.addFailurePlan(plan);
            planService.insertPlan(plan);
            return Result.ok("编辑成功");
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 异常/策略详情2-调度日志
     *
     * @param eventId
     * @return
     */
    @PostMapping("/planTwo")
    public Result planList(@RequestParam(required = false) String eventId) {
        try {
            List<WarningEventDispatchData> list = warningEventService.findDispatchListByEventIdNoPage(eventId);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    /**
     * 异常/策略详情1-列表
     *
     * @param type    type=1故障预案，2检修情况，3操作票，4调度日志
     * @param eventId
     * @return
     */
    @PostMapping("/planOne")
    public Result eventDetails(@RequestParam(required = false, defaultValue = "1") Integer type,
                               @RequestParam(required = false, defaultValue = "1") Long page,
                               @RequestParam(required = false, defaultValue = "5") Long size,
                               @RequestParam(required = false) String eventId,
                               @RequestParam(required = false) String planId) {
        try {
            if (type == 1) {
                //Long node_id = 2L;
                //WarningEventFailurePlan plan = warningEventService.recommendPlan(eventId, node_id);
                WarningEventFailurePlan plan = warningEventService.findPlanByPlanId(planId);
                return Result.ok(plan);
            }
            if (type == 2) {
                PageResult info = warningEventService.findJxdByEventId(eventId, page, size);
                return Result.ok(info);
            }
            if (type == 3) {
                PageResult info = warningEventService.findCzpByEventId(eventId, page, size);
                return Result.ok(info);
            }
            if (type == 4) {
                PageResult info = warningEventService.findDispatchByEventId(eventId, page, size);
                return Result.ok(info);
            }
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
        return Result.ok();
    }

    /**
     * 检修单详情
     *
     * @param overhaulId
     * @return
     */
    @PostMapping("/overhaulDetails")
    public Result overhaulDetails(@RequestParam(required = false) String overhaulId) {
        try {
            WarningEventOverhaulDetails overhaulDetails = warningEventOverhaulService.findByOverhaulId(overhaulId);
            return Result.ok(overhaulDetails);
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }

    /**
     * 预案详情
     *
     * @param planId
     * @return
     */
    @PostMapping("/planDetails")
    public Result planDetails(@RequestParam(required = false) String planId) {
        try {
            WarningEventFailurePlan warningEventFailurePlan = warningEventService.findPlanByPlanId(planId);
            return Result.ok(warningEventFailurePlan);
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    @PostMapping("/testT0121IALMFINDEX")
    public Result testT0121IALMFINDEX(@RequestParam(required = false) Integer localDate) {
        try {
            if (null == localDate) {
                LocalDate localDate2 = LocalDate.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
                Integer localDate3 = Integer.valueOf(localDate2.format(formatter));
                warningEventService.findexAnalysis(localDate3);
            } else {
                warningEventService.findexAnalysis(localDate);
            }
            return Result.ok();
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }

    @PostMapping("/testT0125IALMWINDEX")
    public Result testT0125IALMWINDEX() {
        try {
            warningEventService.windexAnalysis();
            return Result.ok();
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }

    /**
     * 预案推荐多条
     *
     * @param eventId
     * @return
     */
    @PostMapping("/planRecommend")
    public Result planRecommend(String eventId) {
        try {
            List<WarningEventFailurePlanDto> planList = warningEventService.recommend(eventId);
            return Result.ok(planList);
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 批量导入 故障预案基础数据到mysql 预案库
     *
     * @param file
     * @return
     */
    @PostMapping("/updatePlan")
    public Result batchWordImport(@RequestParam(required = false) MultipartFile file) {
        try {
            warningEventService.batchUploadWordPlan(file);
            return Result.ok("更新成功");
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }

}